﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Staticage.Attributes;

namespace System
{
    public static class AttachDataExtensions
    {
        public static object GetAttachedData(
            this ICustomAttributeProvider provider, object key) {
            var attributes = (AttachDataAttribute[])provider.GetCustomAttributes(
                typeof(AttachDataAttribute), false);
            return attributes.First(a => a.Key.Equals(key)).Value;
        }

        public static T GetAttachedData<T>(
            this ICustomAttributeProvider provider, object key) {
            return (T)provider.GetAttachedData(key);
        }

        public static object GetAttachedData(this Enum value, object key) {
            return value.GetType().GetField(value.ToString()).GetAttachedData(key);
        }

        public static T GetAttachedData<T>(this Enum value, object key) {
            return (T)value.GetAttachedData(key);
        }

        public static string GetSummary(this Enum value) {
            return GetAttachedData<string>(value, AttachDataType.Text);
        }
    }
}
